package com.mask.im.business.strategy.impl.sendcode;

import com.mask.im.business.model.dto.EmailDTO;
import com.mask.im.business.service.RedisService;
import com.mask.im.business.common.util.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static com.mask.im.business.common.constant.RedisConstant.*;


@Service("emailPasswordRecoverySendCodeImpl")
public class EmailPasswordRecoverySendCodeImpl extends AbstractSendCode {

	@Autowired
	private RedisService redisService;

	@Autowired
	private TaskExecutor taskExecutor;

	@Autowired
	private EmailUtil emailUtil;

	@Override
	public void sendCode(String email) {
		//检查邮箱和验证码
		checkEmailAndCode(email,EMAIL_PASSWORD_RECOVERY);
		//生成验证码
		String code = getCode();
		taskExecutor.execute(() -> {
			Map<String, Object> map = new HashMap<>();
			map.put("content", "《密码找回》您的验证码为: <span style=\"font-weight:700\">" + code + "</span> ,请注意查收,5分钟内有效。");
			EmailDTO emailDTO = EmailDTO.builder()
					.template("common.html")
					.email(email)
					.code(code)
					.subject("密码找回验证码")
					.commentMap(map).build();
			emailUtil.sendHtmlMail(emailDTO);
			//将验证码放入redis
			redisService.set(EMAIL_PASSWORD_RECOVERY + email, code, 5 * 60);
		});
	}
}
